﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Common;
using System.Data.SqlClient;

/// <summary>
/// Summary description for CtrlBDLogin
/// </summary>
public class CtrlBDLogin
{
    String strConnect;
    String strSql;
    SqlConnection connection;
    SqlCommand command;

	public CtrlBDLogin()
	{
        strConnect = ConfigurationManager.ConnectionStrings["conexion"].ConnectionString;
        connection = new SqlConnection(strConnect);
	}

    public string[] userExists(string userName, string password)
    {
        bool exists = false;

        connection.Open();
        strSql = "EXEC returnUser '" + userName + "', '" + password + "'";
        SqlDataReader dataReader = null;
        command = new SqlCommand(strSql, connection);
        dataReader = command.ExecuteReader();

        string[] userData = new string[6];

        if (dataReader != null)
        {
            if (dataReader.HasRows == true)
            {
                while (dataReader.Read())
                {
                    userData[0] = (string)(dataReader["UserName"]);
                    userData[1] = (string)(dataReader["First_Name"]);
                    userData[2] = (string)(dataReader["Last_Name"]);
                    userData[3] = (dataReader["Profile"]).ToString();
                    userData[4] = (dataReader["Role"]).ToString();
                    userData[5] = (dataReader["Id"]).ToString();
                }
                exists = true;
            }
        }

        connection.Close();
        if (exists)
        {
            return userData;
        }
        else
        {
            return null;
        }
    }
}
